package cn.wp.common.core.model;

import cn.wp.common.core.exception.WpServiceException;
import cn.wp.common.core.util.BeanUtil;
import lombok.Data;

import java.util.List;

/**
 * 分页传输对象
 * Created by chenyb on 2019/09/29
 */
@Data
public class WpPage<T> {

    public WpPage() {

    }

    public WpPage(int page, int limit) {
        this.page = page;
        this.limit = limit;
    }

    public WpPage(int total, List<T> list) {
        this.total = total;
        this.list = list;
    }

    public <E> WpPage<E> doConvertDto(Class<E> dtoClass){
        try {
            List<E> dtoList = BeanUtil.listCopyProperties(list,dtoClass);
            WpPage<E> wpPage = new WpPage<>();
            wpPage.setList(dtoList);
            wpPage.setTotal(total);
            wpPage.setLimit(limit);
            wpPage.setPage(page);
            return wpPage;
        }catch (Exception ex){
            throw new WpServiceException("WpPage转换错误",ex);
        }
    }

    public <E> WpPage<E> doConvertDto(Class<E> dtoClass,BeanUtil.Processor processor){
        try {
            List<E> dtoList = BeanUtil.listCopyProperties(list,dtoClass,processor);
            WpPage<E> wpPage = new WpPage<>();
            wpPage.setList(dtoList);
            wpPage.setTotal(total);
            wpPage.setLimit(limit);
            wpPage.setPage(page);
            return wpPage;
        }catch (Exception ex){
            throw new WpServiceException("WpPage转换错误",ex);
        }
    }

    private int page;

    private int limit;

    private int total;

    private List<T> list;

}
